package com.mk.dao.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import java.time.LocalDateTime;
import java.util.Objects;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author XueJinCha
 */
@Component
public class LabMetaObjectHandler implements MetaObjectHandler {

    private static final String MODIFY_TIME = "modifyTime";
    private static final String CREATE_TIME = "createTime";
    private static final String IS_DEL = "isDel";

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        Object gmtCreate = this.getFieldValByName(CREATE_TIME, metaObject);
        if (Objects.isNull(gmtCreate)) {
            this.setFieldValByName(CREATE_TIME, now, metaObject);
        }

        Object gmtModified = this.getFieldValByName(MODIFY_TIME, metaObject);
        if (Objects.isNull(gmtModified)) {
            this.setFieldValByName(MODIFY_TIME, now, metaObject);
        }

        Object isDel = this.getFieldValByName(IS_DEL, metaObject);
        if (Objects.isNull(isDel)) {
            this.setFieldValByName(IS_DEL, 0, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        LocalDateTime now = LocalDateTime.now();
//        this.setFieldValByName(MODIFY_TIME, now, metaObject);
    }
}
